package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public int dcreateOrders(Orders orders) throws SQLException {
        int i = 0;
        String sql = "insert into orders(userid,businessid,daid,ordertotal,orderDate) values (?,?,?,?,?)";
        con = DBUtil.getConnection();
        try {
            pst = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1,orders.getUserId());
            pst.setInt(2,orders.getBusinessId());
            pst.setInt(3,orders.getDaId());
            pst.setDouble(4,orders.getOrderTotal());
            pst.setString(5, CommonUtil.getCurrentDate());
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            while (rs.next()){
                i = rs.getInt(1);
            }
        }finally {
            DBUtil.close(rs,pst);
        }

        return i;
    }

    @Override
    public Orders dgetOrdersById(Integer orderid) throws SQLException {
        Orders orders = new Orders();
        String sql = "select * from orders o,business b where o.businessid = b.businessid and orderid=?";
        con = DBUtil.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderid);
            rs = pst.executeQuery();
            while (rs.next()){

                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
            }
        }finally {
            DBUtil.close(rs,pst);
        }

        return orders;
    }

    @Override
    public List<Orders> dlistOrdersByUserId(String userid) throws SQLException {
        List<Orders> list = new ArrayList<>();
        String sql = "select * from orders o,Business b where o.businessid = b.businessid and userid=?";
        con = DBUtil.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1,userid);
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
                list.add(orders);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
